﻿using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entitys;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RentHouse.Service
{

    public class RolesService : IRolesService
    {

        DBModel _dbContext;
        public RolesService(DBModel dbContext)
        {
            this._dbContext = dbContext;
        }

        /// <summary>
        /// 角色列表展示（分页）
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        Dictionary<string, object> IRolesService.GetList(int page, int limit)
        {
            List<RolesDTO> list =  _dbContext.T_Roles.Select(e => new RolesDTO
            {
                Id = e.Id,
                Name = e.Name,
                CreateDateTime = e.CreateDateTime,
                IsDeleted = e.IsDeleted
            }).ToList();

            //字典类（用于存储数据）
            Dictionary<string, object> map = new Dictionary<string, object>();

            map.Add("code", "0");//数据格式或类型
            map.Add("msg", "用户列表信息");//数据声明
            map.Add("count", list.Count);//数据总数（用于分页）

            map.Add("data", list
                .OrderBy(u => u.Id)//分页前必须根据某一列进行排序，保证数据顺序唯一
                .Skip((page - 1) * limit)//执行当前页起始数据
                .Take(limit));//每页条数

            return map;
        }

        /// <summary>
        /// 角色信息添加
        /// </summary>
        /// <param name="rolesDTO"></param>
        /// <returns></returns>
        string IRolesService.Create(RolesDTO rolesDTO)
        {

            T_Roles t_Roles = new T_Roles()
            {
                Name = rolesDTO.Name ?? "1",
                CreateDateTime = DateTime.Now,
                IsDeleted = true
            };
            _dbContext.T_Roles.AddOrUpdate(t_Roles);
            int i = _dbContext.SaveChanges();

            if (i <= 0)
            {
                return "error";
            }
            else
            {
                return "succeed";
            }
        }
        /// <summary>
        /// 角色信息删除
        /// </summary>
        /// <param name="rolesDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        string IRolesService.Delete(RolesDTO rolesDTO)
        {

            int i;
            T_Roles t_Roles = _dbContext.T_Roles.Where(t => t.Id == rolesDTO.Id).FirstOrDefault();
            _dbContext.Entry(t_Roles).State = EntityState.Deleted;
            _dbContext.T_Roles.Remove(t_Roles);
            i = _dbContext.SaveChanges();
            if (i > 0)
            {
                return "succeed";
            }
            else
            {
                return "error";
            }
        }


        /// <summary>
        /// 角色信息修改T_Permissions
        /// </summary>
        /// <param name="rolesDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        string IRolesService.Update(RolesDTO rolesDTO)
        {


            T_Roles t_Roles = _dbContext.T_Roles.Where(t => t.Id == rolesDTO.Id).FirstOrDefault();

            t_Roles.Name = rolesDTO.Name ?? t_Roles.Name;

            _dbContext.T_Roles.AddOrUpdate(t_Roles);
            int i = _dbContext.SaveChanges();

            if (i <= 0)
            {
                return "error";
            }
            else
            {
                return "succeed";
            }



        }
    }
}
